﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataEntity;
using System.Collections.ObjectModel;
using Model;

namespace SWA_View
{
    public partial class RRT : Form
    {
        int _ProjectID = 0;
        public ObservableCollection<ChiShuiEntity> ChiShuiEntitys = null;
        public ObservableCollection<ExperimentCombinationEntity> Combination = null;
        public LWATExperimentEntity Entity = new LWATExperimentEntity();
        public RRT(int ProjectID)
        {
            InitializeComponent();
            _ProjectID = ProjectID;
            CommonModel model = new CommonModel();

            ChiShuiEntitys = model.Query<ChiShuiEntity>("ProjectID =" + _ProjectID + " and ExperimentType='" + ExperimentType.其他零速横摇自由衰减RRT.ToString() + "'");
           
            Entity = model.Query<LWATExperimentEntity>("ProjectID =" + _ProjectID + " and ExperimentType='" + ExperimentType.其他零速横摇自由衰减RRT.ToString() + "'").FirstOrDefault();
            if (Entity == null)
                Entity = new LWATExperimentEntity() { ProjectID = _ProjectID, ExperimentType = ExperimentType.其他零速横摇自由衰减RRT.ToString() };
            Entity.ChiShuiEntitys = ChiShuiEntitys;
            ChiShuiEntitys.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(ChiShuiEntitys_CollectionChanged);

            ChiShuiEntitys_CollectionChanged(null, null);
            
            SetData(Entity);
        }
        public void SetData(LWATExperimentEntity entiyt)
        {
            txtPaishuiTiji.Text = entiyt.Volume + "";
            txtChuanboZhongxinGaodu.Text = entiyt.Height + "";
            txtChuanboZhongxinZongxiangWeizhi.Text = entiyt.Location;
            txtChuanboZongxiangGuanxingBanjing.Text = entiyt.YSemi + "";
            txtChuanboHengxiangGuanxingBanjing.Text = entiyt.XSemi + "";
        }

        public void GetData()
        {
            Entity.Volume = float.Parse(txtPaishuiTiji.Text);
            Entity.Height = float.Parse(txtChuanboZhongxinGaodu.Text);
            Entity.Location = Entity.Location;
            Entity.YSemi = float.Parse(txtChuanboZongxiangGuanxingBanjing.Text);
            Entity.XSemi = float.Parse(txtChuanboHengxiangGuanxingBanjing.Text);
        }

        private void txtRealLength_TextChanged(object sender, EventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb != null)
            {
                try
                {
                    double.Parse(tb.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("只能输入数字");
                    tb.Focus();
                    tb.Text = "0";
                }
            }
        }

      

        void ChiShuiEntitys_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            listViewDraft.Items.Clear();
            if (ChiShuiEntitys.Count > 0)
            {
                ListViewItem item = null;
                foreach (var c in ChiShuiEntitys)
                {
                    item = new ListViewItem(c.ShouChiShui + "");
                    item.Tag = c;
                    item.SubItems.Add(c.WeiChiShui + "");
                    item.SubItems.Add(c.DSC);
                    listViewDraft.Items.Add(item);
                }
            }
        }

      
        private List<int> DelChiShuiEntitys = new List<int>();

        private void btnCancel_Click(object sender, EventArgs e)
        {

            DialogResult rsltEnsure = MessageBox.Show("确定要退出吗？", "确认关闭", MessageBoxButtons.OKCancel);
            if (rsltEnsure == DialogResult.OK)
            {

                this.DialogResult = DialogResult.Cancel;
                //this.Close();
            }
            else
            {

            }
        }

        private void btnDraft_Click(object sender, EventArgs e)
        {
            DraftForm draftDlg = new DraftForm(0, _ProjectID, DataEntity.ExperimentType.其他零速横摇自由衰减RRT);
            DialogResult rsltDraft = draftDlg.ShowDialog(this);
            if (rsltDraft == DialogResult.OK)
            {
                ChiShuiEntitys.Add(draftDlg.Entity);


            }
        }
        private string GetIDStr(List<int> IDs)
        {
            if (IDs.Count > 0)
            {
                string idstr = IDs[0] + " ";
                for (int i = 1; i < IDs.Count; i++)
                {
                    idstr += " ," + IDs[i];
                }
                return idstr;
            }
            return "";
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (ChiShuiEntitys.Count <= 0 )
            {
                return;
            }
            GetData();
            CommonModel model = new CommonModel();
            model.Save<LWATExperimentEntity>(Entity);
            if (DelChiShuiEntitys.Count > 0)
                model.delete("ChiShuiEnity", " id in (" + GetIDStr(DelChiShuiEntitys) + ")");
            Combination = new ObservableCollection<ExperimentCombinationEntity>();
            foreach (var a in ChiShuiEntitys)
            {
                
                        ExperimentCombinationEntity entity = null;
                        if ( a.ID > 0)
                            entity = model.Query<ExperimentCombinationEntity>("ExperimentType='" + ExperimentType.其他零速横摇自由衰减RRT
                                + "' and ProjectID" + _ProjectID + " and DataCombination='" + Entity.Text + a.Text  + "'").FirstOrDefault();
                        if (entity == null)
                            entity = new ExperimentCombinationEntity()
                            {
                                ProjectID = _ProjectID,
                                ExperimentType = ExperimentType.其他零速横摇自由衰减RRT.ToString(),
                                DataCombination = Entity.Text + a.Text ,
                                CreateDate = DateTime.Now,
                                IsFinished = "否",
                                IsIng = "否"
                            };
                        Combination.Add(entity);
                        model.Save<ChiShuiEntity>(a);
                
            }
            this.DialogResult = DialogResult.OK;

        }

        private void btnDraftDel_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listViewDraft.SelectedItems)
            {
                ChiShuiEntity entity = item.Tag as ChiShuiEntity;
                if (entity != null)
                {
                    ChiShuiEntitys.Remove(entity);
                    if (entity.ID > 0)
                        DelChiShuiEntitys.Add(entity.ID);
                }

            }
        }

        private void RRT_Load(object sender, EventArgs e)
        {
           
        }
    }
}
